package com.kristou.urgdroid;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;

import com.android.looper.DrawableObject;
import com.android.looper.LoopAdapter;
import com.kristou.urgdriver.sensor.Capture.CaptureData;
import com.kristou.urgdriver.sensor.Capture.CaptureSettings;
import com.kristou.urgdriver.sensor.RangeSensor;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kristou on 13/10/03.
 */
public class MeasurementAdapter implements LoopAdapter {

    private static final String TAG = MeasurementAdapter.class.getSimpleName();

    List<DrawableObject> drawableObjects = new ArrayList<DrawableObject>();

    private MeasurementObject mDistances = new MeasurementObject();
    private MeasurementObject mIntensities = new MeasurementObject();
    private RangeSensor mSensor = null;
    private boolean mStarted = false;

    public MeasurementAdapter() {
        drawableObjects.add(mDistances);
        drawableObjects.add(mIntensities);
    }

    @Override
    public void update(long elapsedTime) {

//        mDistances.setData(getDammyDistances());

        if (mStarted && mSensor != null && mSensor.isConnected()) {
            CaptureData data = mSensor.capture();
            if (data != null) {
                mDistances.setData(getDistances(data));
            }
        }
    }

    private ArrayList<PointF> getDammyDistances() {
        ArrayList<PointF> distances = new ArrayList<PointF>();
        distances.add(new PointF(100, 100));
        distances.add(new PointF(100, -100));
        distances.add(new PointF(-100, 100));
        distances.add(new PointF(-100, -100));
        return distances;
    }

    private ArrayList<PointF> getDistances(CaptureData data) {
        ArrayList<PointF> distances = new ArrayList<PointF>();
        for (int i = 0; i < data.steps.size(); i++) {
            CaptureData.Step step = data.steps.get(i);
            float x = (float) (step.distances.get(0) * Math.cos(mSensor.index2rad(data.startStep + i)));
            float y = (float) (step.distances.get(0) * Math.sin(mSensor.index2rad(data.startStep + i)));
            distances.add(new PointF(x, y));
        }

        return distances;
    }

    @Override
    public List<DrawableObject> getDrawableObjects() {
        return drawableObjects;
    }

    @Override
    public void drawBackground(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
    }

    public void setSensor(RangeSensor sensor) {
        this.mSensor = sensor;
        if (mSensor != null && mSensor.isConnected()) {
            mSensor.setCaptureMode(CaptureSettings.CaptureMode.GD_Capture_mode);
        }
    }

    public RangeSensor getSensor() {
        return mSensor;
    }

    public boolean start() {
        if (mSensor != null && mSensor.isConnected()) {
            mStarted = mSensor.startCapture();
        }
        return mStarted;
    }

    public boolean stop() {
        if (mSensor != null && mSensor.isConnected()) {
            mStarted = !mSensor.stopCapture();
        }
        return mStarted;
    }

    public void setDistanceColor(int diatnceColor) {
        mDistances.setColor(diatnceColor);
    }
}
